﻿using MxWeiXinPF.Common;
using MxWeiXinPF.Web.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class grade_edit : ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        protected MxWeiXinPF.Model.manager admin_info = null;
        protected Button btnSubmit;
        protected DropDownList ddlGrade;
        protected HtmlForm form1;
        protected HtmlInputHidden hwid;
        private int id = 0;
        protected TextBox txtRate;
        protected TextBox txtTitle;
        private int wid = 0;

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MxWeiXinPF.BLL.user_grade _grade = new MxWeiXinPF.BLL.user_grade();
            if (this.ddlGrade.SelectedValue.Equals("0"))
            {
                base.JscriptMsg("请选择级别！", "", "Error");
            }
            else if (this.txtRate.Text.Trim().ToString().Equals(""))
            {
                base.JscriptMsg("分配比例不能为空！", "", "Error");
            }
            else if ((decimal.Parse(this.txtRate.Text.ToString().Trim()) <= 0M) || (decimal.Parse(this.txtRate.Text.Trim()) >= 100M))
            {
                base.JscriptMsg("分配比例错误！，必须是大于0.00小于99.99的数字", "", "Error");
            }
            else if (this.action == MXEnums.ActionEnum.Edit.ToString())
            {
                base.ChkAdminLevel("user_grade", MXEnums.ActionEnum.Edit.ToString());
                if (!this.DoEdit(this.id))
                {
                    base.JscriptMsg("保存过程中发生错误！", "", "Error");
                }
                else
                {
                    base.JscriptMsg("修改级别成功！", "grade_list.aspx", "Success");
                }
            }
            else
            {
                base.ChkAdminLevel("user_grade", MXEnums.ActionEnum.Add.ToString());
                if (!_grade.ExistsOrderNumber(this.admin_info.id, this.wid, this.ddlGrade.SelectedValue.ToString().Trim()))
                {
                    if (!this.DoAdd())
                    {
                        base.JscriptMsg("保存过程中发生错误！", "", "Error");
                    }
                    else
                    {
                        base.JscriptMsg("添加级别成功！", "grade_list.aspx", "Success");
                        this.txtRate.Text = "";
                        this.txtTitle.Text = "";
                        this.ddlGrade.SelectedIndex = 0;
                    }
                }
                else
                {
                    base.JscriptMsg("相同级别不能存在二条记录，如果要修改比例，请在列表中选中记录再编辑！", "", "Error");
                }
            }
        }

        private bool DoAdd()
        {
            bool flag = false;
            MxWeiXinPF.Model.user_grade model = new MxWeiXinPF.Model.user_grade();
            MxWeiXinPF.BLL.user_grade _grade2 = new MxWeiXinPF.BLL.user_grade();
            model.grade = this.txtTitle.Text.Trim();
            if (!this.txtRate.Text.Trim().Equals(""))
            {
                model.rate = int.Parse(this.txtRate.Text.Trim());
            }
            if (!this.ddlGrade.SelectedValue.Equals("0"))
            {
                model.ordernumber = int.Parse(this.ddlGrade.SelectedValue.ToString().Trim());
            }
            model.wid = this.wid;
            if (this.admin_info == null)
            {
                this.admin_info = base.GetAdminInfo();
            }
            model.shopid = this.admin_info.id;
            model.company = this.admin_info.company;
            if (_grade2.Add(model) > 0)
            {
                base.AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加级别:" + model.grade);
                flag = true;
            }
            return flag;
        }

        private bool DoEdit(int _id)
        {
            bool flag = false;
            MxWeiXinPF.BLL.user_grade _grade = new MxWeiXinPF.BLL.user_grade();
            MxWeiXinPF.Model.user_grade model = _grade.GetModel(_id);
            model.grade = this.txtTitle.Text.Trim();
            if (!this.txtRate.Text.Trim().Equals(""))
            {
                model.rate = decimal.Parse(this.txtRate.Text.Trim());
            }
            if (!this.ddlGrade.SelectedValue.Equals("0"))
            {
                model.ordernumber = int.Parse(this.ddlGrade.SelectedValue.ToString().Trim());
            }
            model.wid = int.Parse(this.hwid.Value.ToString().Trim());
            if (this.admin_info == null)
            {
                this.admin_info = base.GetAdminInfo();
            }
            model.shopid = this.admin_info.id;
            model.company = this.admin_info.company;
            if (_grade.Update(model))
            {
                base.AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改级别:" + model.grade);
                flag = true;
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string queryString = MXRequest.GetQueryString("action");
            MxWeiXinPF.Model.wx_userweixin weiXinCode = base.GetWeiXinCode();
            this.admin_info = base.GetAdminInfo();
            this.wid = weiXinCode.id;
            if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
            {
                this.action = MXEnums.ActionEnum.Edit.ToString();
                this.id = MXRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    base.JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new MxWeiXinPF.BLL.user_grade().Exists(this.id))
                {
                    base.JscriptMsg("级别不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!this.Page.IsPostBack)
            {
                base.ChkAdminLevel("user_grade", MXEnums.ActionEnum.View.ToString());
                if (this.action == MXEnums.ActionEnum.Edit.ToString())
                {
                    this.ShowInfo(this.id);
                }
            }
        }

        private void ShowInfo(int _id)
        {
            MxWeiXinPF.Model.user_grade model = new MxWeiXinPF.BLL.user_grade().GetModel(_id);
            this.txtTitle.Text = model.grade;
            this.txtRate.Text = model.rate.ToString();
            if ((model.ordernumber.ToString() != null) && (this.ddlGrade.Items.FindByValue(model.ordernumber.ToString().Trim()) != null))
            {
                this.ddlGrade.SelectedIndex = -1;
                this.ddlGrade.Items.FindByValue(model.ordernumber.ToString().Trim()).Selected = true;
            }
            this.hwid.Value = model.wid.ToString();
        }
    }
}